package top.codervan.yygh.hosp.utils;

import top.codervan.yygh.common.helper.HttpRequestHelper;
import top.codervan.yygh.common.utils.MD5;
import top.codervan.yygh.hosp.service.HospitalSetService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: van
 * @Date: 2022/2/19 12:48
 */
public class HospApiUtil {

    /**
     * 获取传递过来医院信息,进行转换
     * @param request
     * @return
     */
    public static Map<String, Object> toParamMap(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        return paramMap;
    }

    /**
     * 验证签名
     * @param paramMap
     * @param hospitalSetService
     * @return
     */
    public static boolean checkSign(Map<String, Object> paramMap, HospitalSetService hospitalSetService){
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");
        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4 判断签名是否一致
        return hospSign.equals(signKeyMd5);
    }

}
